// pages/tonguetwisters/tonguetwisters.js show api
var uitls = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    contentList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.gettonglist()
  },

  bindload: function (event) {
    console.log(event)
  },

  binderror: function (event) {
    console.log(event)
  },
  
  gettonglist: function() {
    var that = this;
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      data: {
        // showapi_timestamp: uitls.initTimeFormat("yyyy-MM-ddhh:mm:ss"),
        showapi_appid: "80365",
        showapi_sign: "10fddd22eac54d6cb27ad19ee63bb36b",
        showapi_res_gzip: "0",
        title: "",
        page: that.data.page
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      url: 'https://route.showapi.com/1623-1',
      method: "POST",
      success: function (res) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        let code = res.data.showapi_res_code
        if (code == 0) {
          var contentList = []
          contentList = res.data.showapi_res_body.contentlist
          contentList.reverse()
          if (that.data.page > 1) {
            contentList = that.data.contentList.concat(contentList)
          }

          that.setData({
            contentList: contentList
          }) 
        }
        else {
          wx.showToast({
            title: res.data.showapi_res_error,
            icon: 'none'
          })
        }
      },
      fail: function (error) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        console.log(error)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      contentList: []
    })
    this.gettonglist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    this.gettonglist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})